package cj.web.yc.controllers;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.web.admin.constants.Constants;
import cj.web.admin.utils.DateUtils;
import cj.web.admin.utils.ReadConfigProperties;
import cj.web.admin.utils.ReturnUUID;
import cj.web.login.controllers.BaseController;
import cj.web.yc.constants.PayWayEnum;
import cj.web.yc.constants.VideoTypeEnum;
import cj.web.yc.domain.Video;
import cj.web.yc.service.VideoService;

@Path("/video")
public class VideoController extends BaseController{

	private static Logger logger = Logger.getLogger(VideoController.class.getName());
	
	@Autowired
	private VideoService videoService;
	
	@Get("show")
	public String show(Model model) {
		return "/views/yc/videoList.vm";
	}

	@Post("getList")
	public String queryList(Video video,Invocation inv) {
		//当前页  
		video.setStart((video.getPage()-1)*video.getRows());
		video.setLength(video.getRows());
		
		JSONObject jo = new JSONObject();
		
		Map rep = videoService.query(video,inv);
		List<Video> objs = (List<Video>) rep.get("videolis");
		int size = (int) rep.get("size");
		
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		model.add("videoTypeEnum",EnumSet.allOf(VideoTypeEnum.class));
		return "/views/yc/videoDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Video video,HttpServletRequest request,@Param("uploadimage") MultipartFile file)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		video.setCategoryIds(video.getCategoryIdIn());
		video.setCategoryDescs(video.getCategoryDescs());
		video.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		video.setCreateAt(DateUtils.getNowTime());
		video.setCreateUser(returnSessionObj(request).getAccount());
		video.setUpdateAt(DateUtils.getNowTime());
		video.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			
			// 判断文件是否为空  
	        if (!file.isEmpty()) {  
	            try {  
	                // 文件保存路径  
	            	String path = ReadConfigProperties.getValue("uploadFilePath");
	            	//判断文件是否存在，不存在创建文件
	            	Map rep = ReturnUUID.getFolder(path);
	            	path = rep.get("allpath").toString();//所在的全路径
	            	//文件名
	                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
	                // 转存文件  
	                file.transferTo(new File(path+"/"+fileName));  
	                jo.put("filePath", rep.get("filepath")+"/"+fileName);
	                
	                video.setImg(rep.get("filepath")+"/"+fileName);//文件路径
	            } catch (Exception e) {  
	                e.printStackTrace();  
	                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
	            }  
	        } 
			
			videoService.create(video);
		}catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Video video = videoService.queryById(id);

		model.add("video", video);
		model.add("videoTypeEnum",EnumSet.allOf(VideoTypeEnum.class));
		return "/views/yc/videoEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Video video,HttpServletRequest request,@Param("uploadimage") MultipartFile file)
	{
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		video.setCategoryIds(video.getCategoryIdIn());
		video.setCategoryDescs(video.getCategoryDescs());
		video.setUpdateAt(DateUtils.getNowTime());
		video.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			
			// 判断文件是否为空  
	        if (!file.isEmpty()) {  
	            try {  
	                // 文件保存路径  
	            	String path = ReadConfigProperties.getValue("uploadFilePath");
	            	//判断文件是否存在，不存在创建文件
	            	Map rep = ReturnUUID.getFolder(path);
	            	path = rep.get("allpath").toString();//所在的全路径
	            	//文件名
	                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
	                // 转存文件  
	                file.transferTo(new File(path+"/"+fileName));  
	                jo.put("filePath", rep.get("filepath")+"/"+fileName);
	                
	                video.setImg(rep.get("filepath")+"/"+fileName);//文件路径
	            } catch (Exception e) {  
	                e.printStackTrace();  
	                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
	            }  
	        } 
			
			videoService.update(video);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			videoService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}